package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.GroupEntity;
import com.kifas.entity.MenuEntity;

@Repository
public class MenuDao {

	@Autowired
	private SessionFactory sessionFactory;

	private static final String ALL_MENU = "FROM MenuEnityt m";
	private static final String MENU_BY_ID = ALL_MENU + " where m.idMenu = :idMenu";
	private static final String MENU_BY_GROUP = ALL_MENU + "where m.group = :group";

	@SuppressWarnings("unchecked")
	public List<MenuEntity> getMenuByGroup(GroupEntity group) {
		return sessionFactory.getCurrentSession().createQuery(MENU_BY_GROUP).setEntity("group", group).list();
	}

	public MenuEntity getMenuById(Long idMenu) {
		return (MenuEntity) sessionFactory.getCurrentSession().createQuery(MENU_BY_ID).setParameter("idMenu", idMenu);
	}
	
	public void insert(MenuEntity menu) throws Exception {
		sessionFactory.getCurrentSession().save(menu);
	}

}
